//------------------------------------------------
// Eliminated URL Management Tool
// Author : Alex Woo Hyun Kim
// Date : Aug. 19 , 2002
// Email : woorung@empal.com
//------------------------------------------------

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "CMyMan.h"
#include "CMyStatement.h"
#include "CStringTokenizer.h"
#include "CWnCgi.h"

int main()
{
	cout << "Content-type: text/html\n\n";
	cout << "<html><body>\n";

	CWnCgi *WnCgi = new CWnCgi();

	CMyMan *MyMan = new CMyMan("MyConMan.conf");
	CMyResultSet *RS=NULL;
	CMyStatement *Stmt;

	// Display the eliminated URLs
	cout << "<center><b><font color=blue>Eliminated URL List View</font></b></center>" << endl;
    cout << "<form method=\"post\" action=\"EliminatedUrlMan.cgi\">" << endl;
	cout << "<table width=600 border=0 cellspacing=0 cellpadding1 align=center>" << endl;
    cout << "<tr><td width=500 colspan=2>URL to eliminate : <input type=text name=eliminated_url size=50 value=http://></td><td width=100 colspan=2><input type=submit value=Add></td></tr>" << endl;
    cout << "</table>" << endl;
    cout << "</form>" << endl;
	cout << "<table width=800 align=center>" << endl; 
	cout << "<tr>" << endl;
	cout << "<td width=800 colspan=2 align=right><a href=\"EliminatedUrlMan.cgi?dumping_file=/data1/SEARCH/WDIRSITE/DATA/E_URL.dat\"><font size=2 color=red><b>Dump</b></font></a></td>" << endl;
	cout << "</tr>" << endl;
	cout << "</table>" << endl;
    cout << "<table width=802 border=0 cellspacing=0 cellpadding=1 align=center>" << endl;
    cout << "<tr>" << endl;
    cout << "<table width=800 border=0 cellspacing=1 cellpadding=10 align=center>" << endl;
    cout << "<tr>" << endl;
    cout << "<td width=600 colspan=2 align=center bgcolor=#cfd0ed><b>Eliminated URL</b></td>" << endl;
	cout << "<td width=130 colspan=2 align=center bgcolor=#cfd0ed><b>Eliminated Date</b></td>" << endl;
	cout << "<td widTH=70 colspan=2 align=center bgcolor=#cfd0ed><b>Action</b></td>" << endl; 
	cout << "</tr>" << endl;

	if(WnCgi->getFormData()==true) {
		char *pszEliminatedUrl = NULL;
		char *pszSearchingUrl = NULL;
		char *pszDeletingUrl = NULL;
		char *pszDumpingFile = NULL;

		pszEliminatedUrl = WnCgi->getEntryDataByEntryName("eliminated_url");
		pszSearchingUrl = WnCgi->getEntryDataByEntryName("searching_url");
		pszDeletingUrl = WnCgi->getEntryDataByEntryName("deleting_url");
		pszDumpingFile = WnCgi->getEntryDataByEntryName("dumping_file");

		if(pszEliminatedUrl!=NULL) {
			Stmt = new CMyStatement("insert into eliminate_url ( url,reg_date ) values ( (?),now() )","(?)");
			Stmt->SetString(1,pszEliminatedUrl);
			RS = MyMan->GetMyResultSet(Stmt);
			if(RS==NULL||RS->GetAffectedRows()<1) {
				cout << "<tr><td><font color=red>Already existed : " << pszEliminatedUrl << "</font></td></tr>" << endl;
			}
			delete Stmt;
			delete RS;
			RS = NULL;
		}
		else if(pszSearchingUrl!=NULL) {
			Stmt = new CMyStatement("select * from eliminate_url where url like \'(?)%\'","(?)");
			Stmt->SetName(1,pszSearchingUrl);
			RS = MyMan->GetMyResultSet(Stmt);
			if(RS==NULL) {
				cout << "<tr><td><font color=red>Error : " << Stmt->GetStatement() << "</font></td></tr>" << endl;
			}
			else if(RS->GetAffectedRows()<1) {
				cout << "<tr><td><font color=red>There is no url to search : " << pszSearchingUrl << "</font></td></tr>" << endl;
			}
			else {
				while(RS->HasMoreRowsFromMyResultSet()) {
					cout << "<tr>" << endl;
					cout << "<td width=600 colspan=2 align=left bgcolor=#fafaee><font size=2 color=red>" << RS->GetString("url") << "</font></td>" << endl;
					cout << "<td width=130 colspan=2 align=left bgcolor=#fafaee><font size=2 color=red>" << RS->GetString("reg_date") << "</font></td>" << endl;
					cout << "<td width=70 colspan=2 align=left bgcolor=#fafaee><font size=2 color=red><a href=EliminatedUrlMan.cgi?deleting_url=" << RS->GetString("url") << ">Delete</a></font></td>" << endl;
				}
			}
			delete Stmt;
			delete RS;
			RS = NULL;
		}
		else if(pszDeletingUrl!=NULL) {
			Stmt = new CMyStatement("delete from eliminate_url where url = (?)","(?)");
			Stmt->SetString(1,pszDeletingUrl);
			RS = MyMan->GetMyResultSet(Stmt);
			if(RS==NULL||RS->GetAffectedRows()<1) {
				cout << "<tr><td><font color=red>Error : " << Stmt->GetStatement() << "</font></td></tr>" << endl;
			}
			delete Stmt;
			delete RS;
			RS = NULL;
		}
		else if(pszDumpingFile!=NULL) {
			char szTmp[256];
			sprintf(szTmp,"mv %s %s.OLD",pszDumpingFile,pszDumpingFile);
			system(szTmp);

			Stmt = new CMyStatement("select url into outfile \"(?)\" from eliminate_url","(?)");
			Stmt->SetName(1,pszDumpingFile);
			RS = MyMan->GetMyResultSet(Stmt);
			if(RS==NULL) {
				cout << "<tr><td><font color=red>Error : " << Stmt->GetStatement() << "</font></td></tr>" << endl;
			}
			else if(RS->GetAffectedRows()<1) {
				 cout << "<tr><td><font color=red>There is no url to dump</font></td></tr>" << endl;
			}
			else {
				cout << "<tr><td><font color=red>completed dump to " << pszDumpingFile << "</font></td></tr>" << endl;
			}
			delete Stmt;
			delete RS;
			RS = NULL;
		}
	}

	RS = MyMan->GetMyResultSet("select * from eliminate_url");
	if(RS==NULL) {
		cout << "<tr><td>Error : select * from eliminate_url</td></tr>" << endl;
	}
	else {
		if(RS->GetAffectedRows()<1) {
			cout << "<tr><td>No Result !!</td></td>" << endl; 
		}
		else {
			while(RS->HasMoreRowsFromMyResultSet()) {
				cout << "<tr>" << endl;
				cout << "<td width=600 colspan=2 align=left bgcolor=#fafaee><font size=2>" << RS->GetString("url") << "</font></td>" << endl;
				cout << "<td width=130 colspan=2 align=left bgcolor=#fafaee><font size=2>" << RS->GetString("reg_date") << "</font></td>" << endl;
				cout << "<td width=70 colspan=2 align=left bgcolor=#fafaee><font size=2><a href=EliminatedUrlMan.cgi?deleting_url=" << RS->GetString("url") << ">Delete</a></font></td>" << endl;
				cout << "</tr>" << endl;
			}
		}
	}
	delete RS;

	cout << "</table>" << endl;
	cout << "</tr>" << endl;
	cout << "</table>" << endl;

	cout << "<form method=\"post\" action=\"EliminatedUrlMan.cgi\">" << endl;
	cout << "<table width=800 border=0 cellspacing=0 cellpadding1 align=center>" << endl;
	cout << "<tr><td width=700 colspan=2 align=right>Searching URL : <input type=text name=searching_url size=50 value=http://></td><td width=100 colspan=2><input type=submit value=Search></td></tr>" << endl;
	cout << "</table>" << endl;
	cout << "</form>" << endl;

	delete MyMan;
	delete WnCgi;

	cout << "</body></html>" << endl;
	return 0;
}
